package com.jake.springboot.calculation.service;

import com.google.common.base.Preconditions;
import com.jake.springboot.calculation.context.CalculationContext;
import com.jake.springboot.calculation.exception.OperationNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CalculationService {

    @Autowired
    private CalculationContext calculationContext;

    public int operateByStrategy(String strategy, int num1, int num2) {
        return calculationContext.getCalculationStrategyMap().get(strategy).operate(num1, num2);
    }

    public int operateByCondition(String operation, int num1, int num2) {
        switch (operation) {
            case "add":
                return num1 + num2;
            case "subtract":
                return num2 - num1;
            case "multiple":
                return num1 * num2;
            case "divide":
                Preconditions.checkArgument(num1 != 0, "除数不应为0");
                return num2 / num1;
        }
        throw new OperationNotFoundException("没有对应的运算");
    }

}
